package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminSetmealController")
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
@Slf4j
public class SetmealController {

    private final SetmealService setmealService;

    @Autowired
    public SetmealController(SetmealService setmealService) {
        this.setmealService = setmealService;
    }

    /**
     * 根据id查询套餐
     *
     * @param id 套餐id
     * @return 套餐详细信息
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> SetmealDetail(@PathVariable Long id) {
        log.info("根据id查询套餐:{}", id);
        return Result.success(setmealService.SetmealDetail(id), MessageConstant.SELECT_OK);
    }


    /**
     * 新增套餐
     *
     * @param setmealDTO 套餐实体
     * @return 提示
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.categoryId", allEntries = true)
    public Result<String> save(@RequestBody SetmealDTO setmealDTO) {
        setmealService.save(setmealDTO);
        return Result.success(MessageConstant.ADD_OK);
    }


    /**
     * 分页查询套餐列表
     *
     * @param queryDTO 套餐查询dto
     * @return 分页后的套餐列表
     */
    @GetMapping("/page")
    @ApiOperation("分页查询套餐列表")
    public Result<PageResult<SetmealVO>> pageSetmealList(SetmealPageQueryDTO queryDTO) {
        log.info("分页查询套餐列表:{}", queryDTO);
        return Result.success(setmealService.pageSetmealList(queryDTO), MessageConstant.SELECT_OK);
    }

    /**
     * 套餐起售、停售
     *
     * @param status 1起售 0停售
     * @return 提示
     */
    @PostMapping("/status/{status}")
    @ApiOperation("套餐起售、停售")
    @CacheEvict(value = "setmealCache", allEntries = true)
    public Result<String> startOrStop(@PathVariable Integer status, Long id) {
        log.info("套餐起售、停售:{},{}", status, id);
        setmealService.startOrStop(status, id);
        return Result.success(MessageConstant.OPERATE_OK);
    }


    /**
     * 修改套餐
     *
     * @param setmealDTO 修改套餐dto
     * @return 提示
     */
    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.categoryId", allEntries = true)
    public Result<String> update(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐:{}", setmealDTO);
        setmealService.update(setmealDTO);
        return Result.success(MessageConstant.UPDATE_OK);
    }


    /**
     * 批量删除套餐
     *
     * @param ids id列表
     * @return 提示
     */
    @DeleteMapping()
    @ApiOperation("批量删除套餐")
    @CacheEvict(value = "setmealCache", allEntries = true)
    public Result<String> deletes(@RequestParam List<Long> ids) {
        log.info("批量删除套餐:{}", ids);
        setmealService.deleteBath(ids);
        return Result.success(MessageConstant.OPERATE_OK);
    }
}
